
classdef SocketClientForSMA
    properties
        port
        host
        clientSocket
        iStream
        oStream
    end
    
    methods
        function this = SocketClientForSMA(port,host)
            import java.net.*;
            import java.io.*;
            
            this.port = port;
            this.host;
            disp('Connecting to server...')
            this.clientSocket = Socket(host, this.port);
            
            this.iStream = this.clientSocket.getInputStream;
            this.oStream = this.clientSocket.getOutputStream;
            
            
            
            % Greets the server on connection is accepted
            
            
        end
        
        
        function command = updateD(server,speed1, angle1, speed2, angle2)
            
            
            trame = strcat('CMD,',num2str(int16((angle1))));
            trame = strcat(trame,',');
            trame = strcat(trame,num2str(int16((speed1))));
            trame = strcat(trame,',');
            trame = strcat(trame,num2str(int16((angle2))));
            trame = strcat(trame,',');
            trame = strcat(trame,num2str(int16((speed2))));
            trame = strcat(trame,',');
            length(trame);
            
            for i=length(trame):25
                trame = strcat(trame,num2str(((0))));
            end
            disp(trame);
            server.oStream.sendS(trame);
            
            while ~(server.iStream.available)
                
            end
            cmd = readS(server.iStream);
            disp(cmd);
            C = textscan(cmd, '%s', 'delimiter', ',');
            C=C{1};
            C=C(1);
            S= regexp (C, 'S', 'split');
            S=S{1};
            
            
            A= regexp (S(1), 'A', 'split');
            A=A{1};
            
            S=S(2);
            A=A(2);
            
            command=[A,S];
            
            
        end
    end
end
